(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.test.sampleCompany.updateController', updateController);
	updateController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'sampleCompanyService' ];
	function updateController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, sampleCompanyService) {
		var ctrl = this, companyInfo = $scope.$stateParams.params, errorMessage = {};
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.company = {};
		ctrl.company.subCompanyList = ctrl.company.subCompanyList || [];

		ctrl.init = function() {
			var params = {
				companyCode : companyInfo.companyCode
			};
			ctrl.innerGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.subCompany.tableConfigs.table, rowSelectionChanged);
			ctrl.innerGridOptions.data = 'vm.company.subCompanyList';
			ctrl.dateDisabled = sampleCompanyService.dateDisabled;
			ctrl.siteCodeOptions = sampleCompanyService.getSiteCodeOptions();
			ctrl.companyTypeOptions = sampleCompanyService.getCompanyTypeOptions();
			ctrl.companyTypeLookupOptions = sampleCompanyService.getCompanyTypeLookupOptions();
			ctrl.sensitiveOptions = sampleCompanyService.getSensitiveOptions();
			ctrl.ccyOptions = sampleCompanyService.getCcyOptions();
			ctrl.calendarYear = 2017;
			authService.update(params).then(function(response) {
				ctrl.company = response.data || companyInfo;
				ctrl.company.subCompanyList = ctrl.company.subCompanyList || [];
			});
		};

		ctrl.updateSave = function() {
			var messages = [];
			angular.forEach(errorMessage, function(field) {
				angular.forEach(field, function(rule) {
					messages.push(rule);
				});
			});
			if (messages.length > 0) {
				modalService.openErrorDialogs({
					'messages' : messages
				});
			} else if (ctrl.company.taskId) {
				return authService.updateContinueSave(ctrl.company).then(saveSuccessful, saveFailed);
			} else {
				return authService.updateSave(ctrl.company).then(saveSuccessful, saveFailed);
			}
		};

		ctrl.updateSubmit = function() {
			var messages = [];
			angular.forEach(errorMessage, function(field) {
				angular.forEach(field, function(rule) {
					messages.push(rule);
				});
			});
			if (messages.length > 0) {
				modalService.openErrorDialogs({
					'messages' : messages
				});
			} else if (ctrl.company.taskId) {
				return authService.updateContinueSubmit(ctrl.company).then(submitSuccessful, submitFailed);
			} else {
				return authService.updateSubmit(ctrl.company).then(submitSuccessful, submitFailed);
			}
		};

		ctrl.addSubCompany = function() {
			ctrl.company.subCompanyList.push({
			});
		};

		ctrl.removeSubCompany = function() {
			angular.forEach(ctrl.selectedSubCommpanyList, function(selectedSubCompany, key) {
				angular.forEach(ctrl.company.subCompanyList, function(subCompany, index) {
					if (subCompany._index == selectedSubCompany._index) {
						ctrl.company.subCompanyList.splice(index, 1);
					}
				});
			});
		};

		$scope.$on("COMPANYTYPE2_CANCEL", function(event, param) {
			event.preventDefault();
			event.stopPropagation();
		});

		$scope.$on("COMPANYTYPE2_OK", function(event, param) {
			event.preventDefault();
			event.stopPropagation();
		});

		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedSubCommpanyList = selectedRows;
		}

		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.save' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.test.sampleCompany.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.company.list',
					'state' : 'cda.test.sampleCompany.list',
					'params' : {}
				}]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function saveSuccessful(response) {
			modalService.openInfoDialogs({
				'messages' : [ 'title.dialog.successful.save' ]
			});
			if (response.data.task && response.data.task.taskId) {
				authService.saveSuccessfulProcess(response.data.task).then(function(response) {
					var routerInfo = response.data || {}, state = routerInfo.processState;
					delete routerInfo.processState;
					$scope.go(state, routerInfo);
				}, angular.noop);
			}
		}

		function saveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);